package com.ruoyi.cms.chat.service.impl;

import com.ruoyi.cms.chat.entity.CmsChatHistory;
import com.ruoyi.cms.chat.mapper.CmsChatHistoryMapper;
import com.ruoyi.cms.chat.service.CmsChatHistoryService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (CmsChatHistory)表服务实现类
 *
 * @author makejava
 * @since 2024-09-04 18:41:05
 */
@Service("cmsChatHistoryService")
public class CmsChatHistoryServiceImpl implements CmsChatHistoryService {
    @Resource
    private CmsChatHistoryMapper cmsChatHistoryMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public CmsChatHistory queryById(Integer id) {
        return this.cmsChatHistoryMapper.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param cmsChatHistory 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<CmsChatHistory> queryByPage(CmsChatHistory cmsChatHistory, PageRequest pageRequest) {
        long total = this.cmsChatHistoryMapper.count(cmsChatHistory);
        return new PageImpl<>(this.cmsChatHistoryMapper.queryAllByLimit(cmsChatHistory, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param cmsChatHistory 实例对象
     * @return 实例对象
     */
    @Override
    public CmsChatHistory insert(CmsChatHistory cmsChatHistory) {
        this.cmsChatHistoryMapper.insert(cmsChatHistory);
        return cmsChatHistory;
    }

    /**
     * 修改数据
     *
     * @param cmsChatHistory 实例对象
     * @return 实例对象
     */
    @Override
    public CmsChatHistory update(CmsChatHistory cmsChatHistory) {
        this.cmsChatHistoryMapper.update(cmsChatHistory);
        return this.queryById(cmsChatHistory.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.cmsChatHistoryMapper.deleteById(id) > 0;
    }

    @Override
    public List<CmsChatHistory> queryHistory(Integer fromUsername, Integer targetUsername) {
        return this.cmsChatHistoryMapper.queryHistory(fromUsername, targetUsername);
    }
}
